//
//  TXFEmoticonTools.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/7/5.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit
// 显示的列数
let EmoticonMaxCol = 7
// 显示的行数
let EmoticonMaxRow = 3
// 每页显示的个数
let EmoticonMaxCount = EmoticonMaxCol * EmoticonMaxRow - 1
class TXFEmoticonTools: NSObject {

    //创建单例(工具类,全局访问点)
    
    static let sharedTools :  TXFEmoticonTools = TXFEmoticonTools()
    
    //三个一维数组根据plist名字获取
    //第一个:默认表情
    lazy  var defaultEmotions: [TXFEmoticon] = {
        
        
      return self.getEmoticons("default/")
        
    }()
    //第二个表情
    lazy  var emojiEmotions: [TXFEmoticon] = {
        
        
        return self.getEmoticons("emoji/")
        
    }()
    //第三个表情
    lazy  var lxhEmotions: [TXFEmoticon] = {
        
        
        return self.getEmoticons("lxh/")
        
        
        
    }()
    
    
    //三维数组:
    
    lazy var allEmoticons: [[[TXFEmoticon]]] = {
        
        
        return [
            
            self.getEmoticonsGroup(self.defaultEmotions),
            
            self.getEmoticonsGroup(self.emojiEmotions),
            
            self.getEmoticonsGroup(self.lxhEmotions),
        ]
        
        
        
    }()

    
    //获取路径bundle
    
    
    lazy var emoticonBundle: NSBundle = {
    
    let  path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
    
    return  NSBundle(path: path)!
    
    }()
    
 

    //获取一维数组
    
    
    func getEmoticons(fileName: String) -> [TXFEmoticon]{
    //获取路径
        
    let  file = emoticonBundle.pathForResource("\(fileName)info.plist", ofType: nil)!
        
    
    //plist转成数组
        
        
    let array = NSArray(contentsOfFile: file)!
        
        //获取数组后遍历数组 字典转模型
        
        var tempArray :  [TXFEmoticon] = [TXFEmoticon]()
        
        
        for  dict in  array {
        
        
        
            let e =  TXFEmoticon(dict: dict as! [String : AnyObject])
            e.path = "\(fileName)"
            
            
            tempArray.append(e)
            
            
        
        }
    
 
        return  tempArray

    }
//    // 显示的列数
//    let EmoticonMaxCol = 7
//    // 显示的行数
//    let EmoticonMaxRow = 3
//    // 每页显示的个数
//    let EmoticonMaxCount = EmoticonMaxCol * EmoticonMaxRow - 1
//    
    //定义二维数组的方法:(一维数组转成二维数组)
    
    func getEmoticonsGroup( emoticons: [TXFEmoticon]) -> [[TXFEmoticon]]{
    
    let  pageCount  = (emoticons.count + EmoticonMaxCount - 1)/EmoticonMaxCount
    
    
    //遍历数组:
        //先定义可变数组
        
        var tempArray : [[TXFEmoticon]] =  [[TXFEmoticon]]()
        
        for i in 0..<pageCount{
        
            let loc  = EmoticonMaxCount * i
            
            var len = EmoticonMaxCount
            
            if loc + len > emoticons.count{
            
            
            len = emoticons.count - loc
            
            
            }
            
            let range  = NSMakeRange(loc, len)
            
            let arr = (emoticons as NSArray).subarrayWithRange(range) as! [TXFEmoticon]
            
            
            
            tempArray.append(arr)
        
        
        
        
        }
        
        
        
        
        
        return tempArray
    
    
    
    
    }
    
    

    
    func getSearchEmoticon(chs: String) -> TXFEmoticon?{
    
    
        for e in defaultEmotions{
        
            if e.chs == chs {
            
            return e
            
            }
        
        }
    
   
        
        for e in lxhEmotions{
            
            if e.chs == chs {
                
                return e
                
            }
            
        }
        
       return nil
    
    
    }
    
    
    
    
    
    
    
    
}
